﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Threading;
using AutoMapper;
using Sautom.ClientComunication.Commands;
using Sautom.ClientComunication.ReportService;
using Sautom.Modules.Report.Controls.ViewModels;
using Sautom.Modules.Report.ViewModels;

namespace Sautom.Modules.Report.Commands
{
    class RequestOrderReportsCommand : CommandBase
    {
        private OrdersReportViewModel ViewModel { get; set; }
        private IReportService ReportesService { get; set; }

        public RequestOrderReportsCommand(OrdersReportViewModel viewModel, IReportService reportesService)
        {
            ViewModel = viewModel;
            ReportesService = reportesService;
        }

        #region Overrides of CommandBase

        public override void Execute()
        {
            Mapper.CreateMap<OrderQueryBuilderViewModel, OrderReportDtoReport>();
            var input = Mapper.Map<OrderReportDtoReport>(ViewModel.BuilderViewModel);
            var uiDispather = Dispatcher.CurrentDispatcher;
            (new Thread(() =>
            {
                var data = ReportesService.OrderReport(input).ToList();

                uiDispather.BeginInvoke(DispatcherPriority.Normal,
                                        (Action)(() => ViewModel.Records = new ObservableCollection<OrderReportDtoReport>(data)));
            })).Start();
        }

        #endregion
    }
}
